package com.cl.controller;

import com.cl.bean.Infor;
import com.cl.bean.Sutils;
import com.cl.bean.Type;
import com.cl.bean.ltype;
import com.cl.service.InforService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.hq.util.FileUploadUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
    CY~
    2021年,05月,24日 8:46
    拨开云雾见天日，守得云开见月明
*/
@Controller
public class InforController {
    @Resource
    private InforService service;

    @RequestMapping("list.do")
    public String list(Model model, @RequestParam(value = "cpage",defaultValue = "1")Integer cpage,
                       String name,String status,String stype,String startTime){
        Map map = new HashMap();
        map.put("name",name);
        map.put("status",status);
        map.put("stype",stype);
        map.put("startTime",startTime);
        PageHelper.startPage(cpage,3);
        List<Map> list = service.list(map);
        PageInfo pi = new PageInfo(list);
        model.addAttribute("pi",pi);
        model.addAttribute("map",map);
        return "list";
    }
    @RequestMapping("del.do")
    @ResponseBody
    public Object del(Integer id){
        int i = service.del(id);
        return i;
    }
    @RequestMapping("tui.do")
    @ResponseBody
    public Object tui(String id){
        int i = service.updtui(id);
        return i;
    }
    @RequestMapping("chu.do")
    @ResponseBody
    public Object chu(String id){
        int i = service.updchu(id);
        return i;
    }
    @RequestMapping("toadd.do")
    public String toadd(Model model){
        ltype[] values = ltype.values();
        model.addAttribute("l",values);
        return "add";
    }
    @RequestMapping("selById.do")
    public String selById(String id,Model model){
        Map map = service.selById(id);
        model.addAttribute("map",map);
        return "update";
    }
    @RequestMapping("addUtils.do")
    public String addUtils(Sutils sutils,Integer rid){
        service.addUtils(sutils,rid);
        return "redirect:list.do";
    }
    @RequestMapping("selType.do")
    @ResponseBody
    public Object selType(){
        List<Type> list = service.selType();
        return list;
    }
    @RequestMapping("add.do")
    public String add(MultipartFile file, HttpSession session, Infor infor,String typeN){
        if(file.getOriginalFilename().length()>0){
            String upload = FileUploadUtil.load(session, file, "upload");
            infor.setPicture("upload/"+upload);
        }
        infor.setType(ltype.valueOf(typeN));
        int add = service.add(infor);
        return "redirect:list.do";
    }
}
